/* echarts
 * {type: 'echarts', option: {}}
 */

var $ = require('dfish'),
	Q = require('jquery'),
	echarts = require('./echarts.min');

define('echarts', echarts);
try { //IE8下会报错 所以try一下
	require('./echarts-wordcloud.min');
} catch(ex){}


define.widget('ECharts', {
	Listener: {
		body: {
			ready: function() {
				this.x.option && this.init(this.x.option);
			},
			resize: function() {
				this.echarts && this.echarts.resize();
			}
		}
	},
	Prototype: {
		init: function(opt) {
			(this.echarts = echarts.init(this.$())).setOption(this.parseOption(opt));
		},
		// 解析 "javscript:return"
		parseOption: function(o) {
			if ($.isArray(o)) {
				for (var i = 0, l = o.length; i < l; i ++) o[i] && typeof o[i] === 'object' && this.parseOption(o[i]);
			} else if (Q.isPlainObject(o)) {
				for (var k in o) {
					var m = o[k];
					if (typeof m === 'string') {
						if (m.indexOf('javascript:') === 0) o[k] = this.formatJS(m);
					} else if ($.isArray(m)) {
						for (var i = 0, l = m.length; i < l; i ++) m[i] && typeof m[i] === 'object' && this.parseOption(m[i]);
					} else if (Q.isPlainObject(m))
						this.parseOption(m);
				}
			}
			return o;
		}
	}
});
